var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function ($stateProvider, $urlRouterProvider) {

  $urlRouterProvider.otherwise('/login');

  $stateProvider
  .state("login", {
    url: "/login",
    templateUrl: "./views/login.template.html"
  })
  .state("home", {
    url: "/home",
    templateUrl: "./views/home.template.html"
  })
  .state("home.main", {
    url: "/main",
    templateUrl: "./views/main.template.html"
  })
  .state("home.goods", {
    url: "/goods",
    templateUrl: "./views/goods.template.html"
  })
  .state("home.barcode", {
    url: "/barcode",
    templateUrl: "./views/barcode.template.html"
  })
  .state("home.color", {
    url: "/color",
    templateUrl: "./views/color.template.html"
  })
  .state("home.size", {
    url: "/size",
    templateUrl: "./views/size.template.html"
  })
  .state("home.class", {
    url: "/class",
    templateUrl: "./views/class.template.html"
  })
  .state("home.style", {
    url: "/style",
    templateUrl: "./views/style.template.html"
  })
  .state("home.brand", {
    url: "/brand",
    templateUrl: "./views/brand.template.html"
  })
  .state("home.sale", {
    url: "/sale",
    templateUrl: "./views/sale.template.html"
  })
  .state("home.returnGoods", {
    url: "/returnGoods",
    templateUrl: "./views/returnGoods.template.html"
  })
  .state("home.purchaseNewGoods", {
    url:"/purchaseNewGoods",
    templateUrl: "./views/purchaseNewGoods.template.html"
  })
  .state("home.purchaseOldGoods", {
    url:"/purchaseOldGoods",
    templateUrl: "./views/purchaseOldGoods.template.html"
  })
  .state("home.myShop", {
    url:"/myShop",
    templateUrl: "./views/myShop.template.html"
  })
  .state("home.addStorage", {
    url:"/addStorage",
    templateUrl: "./views/addStorage.template.html"
  })
  .state("home.stock", {
    url:"/stock",
    templateUrl: "./views/stock.template.html"
  })
  .state("home.accountBook", {
    url:"/accountBook",
    templateUrl: "./views/accountBook.template.html"
  })
  .state("home.monthlySalesReport", {
    url:"/monthlySalesReport",
    templateUrl: "./views/monthlySalesReport.template.html"
  })
  .state("home.classifiedSalesReport", {
    url:"/classifiedSalesReport",
    templateUrl: "./views/classifiedSalesReport.template.html"
  })
  .state("home.addShop", {
    url:"/addShop",
    templateUrl: "./views/addShop.template.html"
  })
  .state("home.position", {
    url:"/position",
    templateUrl: "./views/position.template.html"
  })
  .state("home.addEmployee", {
    url:"/addEmployee",
    templateUrl: "./views/addEmployee.template.html"
  })
  .state("404", {
    url:"/404",
    templateUrl: "./404.html"
  });

});


myApp.controller('initializeData', ['$scope', '$timeout', '$color', '$size', '$class', '$style', '$brand', function($scope, $timeout, $color, $size, $class, $style, $brand){

  // Color的数据初始化及刷新数据方法
  var promise_Color = $color.loadAllColor();
  promise_Color.then(function(data) {
    $color.allColor = data;
    $scope.top_Color = data;
  }, function(n) {
    $color.allColor = [{"name": "数据初始化出错！"}];
    $scope.top_Color = [{"name": "数据初始化出错！"}];
  });

  $scope.refresh_Color = function () {

    var timer = $timeout(
      function() {
        if($scope.top_Color !== $color.allColor) {
          $scope.top_Color = $color.allColor;
          $timeout.cancel(timer);
        }
      },100);
    
    timer.then(
      function() {
        console.log("Color还没更新");
        $scope.refresh_Color();
      },
      function() {
        console.log("Color已更新完成");
      });

  };


  // Size的数据初始化及刷新数据方法
  var promise_Size = $size.loadAllSize();
  promise_Size.then(function(data) {
    $size.allSize = data;
    $scope.top_Size = data;
  }, function(n) {
    $size.allSize = [{"name": "数据初始化出错！"}];
    $scope.top_Size = [{"name": "数据初始化出错！"}];
  });

  $scope.refresh_Size = function () {

    var timer = $timeout(
      function() {
        if($scope.top_Size !== $size.allSize) {
          $scope.top_Size = $size.allSize;
          $timeout.cancel(timer);
        }
      },100);
    
    timer.then(
      function() {
        console.log("Size还没更新");
        $scope.refresh_Size();
      },
      function() {
        console.log("Size已更新完成");
      });

  };


  // Class的数据初始化及刷新数据方法
  var promise_Class = $class.loadAllClass();
  promise_Class.then(function(data) {
    $class.allClass = data;
    $scope.top_Class = data;
  }, function(n) {
    $class.allClass = [{"name": "数据初始化出错！"}];
    $scope.top_Class = [{"name": "数据初始化出错！"}];
  });

  $scope.refresh_Class = function () {

    var timer = $timeout(
      function() {
        if($scope.top_Class !== $class.allClass) {
          $scope.top_Class = $class.allClass;
          $timeout.cancel(timer);
        }
      },100);
    
    timer.then(
      function() {
        console.log("Class还没更新");
        $scope.refresh_Class();
      },
      function() {
        console.log("Class已更新完成");
      });

  };


  // Style的数据初始化及刷新数据方法
  var promise_Style = $style.loadAllStyle();
  promise_Style.then(function(data) {
    $style.allStyle = data;
    $scope.top_Style = data;
  }, function(n) {
    $style.allStyle = [{"name": "数据初始化出错！"}];
    $scope.top_Style = [{"name": "数据初始化出错！"}];
  });

  $scope.refresh_Style = function () {

    var timer = $timeout(
      function() {
        if($scope.top_Style !== $style.allStyle) {
          $scope.top_Style = $style.allStyle;
          $timeout.cancel(timer);
        }
      },100);
    
    timer.then(
      function() {
        console.log("Style还没更新");
        $scope.refresh_Style();
      },
      function() {
        console.log("Style已更新完成");
      });

  };


  // Brand的数据初始化及刷新数据方法
  var promise_Brand = $brand.loadAllBrand();
  promise_Brand.then(function(data) {
    $brand.allBrand = data;
    $scope.top_Brand = data;
  }, function(n) {
    $brand.allBrand = [{"name": "数据初始化出错！"}];
    $scope.top_Brand = [{"name": "数据初始化出错！"}];
  });

  $scope.refresh_Brand = function () {

    var timer = $timeout(
      function() {
        if($scope.top_Brand !== $brand.allBrand) {
          $scope.top_Brand = $brand.allBrand;
          $timeout.cancel(timer);
        }
      },100);
    
    timer.then(
      function() {
        console.log("Brand还没更新");
        $scope.refresh_Brand();
      },
      function() {
        console.log("Brand已更新完成");
      });

  };





}]);